<?php

namespace Admin\Controller;

use Think\Controller;

class ProjectController extends Controller {
    function __destruct(){                                                                         // ��������规��  �����ョ�ㄦ�疯�块��������浣���ュ��
        //require '/home/wwwroot/default/world/behavior.php';
        
    }

  public function showList() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 3 ); // 验证是否登陆
		$number = 1; // 计数器，前台编号累加值
		$showList = M ( 'project' ); // 实例化project
		if (I('post.name')!="") {
		$where['name']=array('like','%'.I('post.name').'%');
		$where['leader']=array('like','%'.I('post.name').'%');	
		$where['_logic']='or';
		$map['_complex']=$where;
		$map['ischecked']=array('eq',1);
		$map['ischecked']=array('eq',2);
		$map['_logic']='or';

		$name=I('post.name');
		$leader=I('post.name');
	    $count=$showList->where($map)->count();
	   
	    $p=getpage($count,8);
	    $through=M()->query("select *  from  project
                                                 left JOIN (
                                                            select  p_id.pid,  group_concat(  users.realname separator ';' ) as joinusers
                                                            from  projectbid p_id,  users  where  p_id.uid = users.uid  and p_id.isjoined = 1  GROUP BY  p_id.pid )
                                                 bid on bid.pid = project.id  where (ischecked = 1 or ischecked = 2) AND (name like '%$name%' or leader like  '%$name%')  order by  adddate desc limit $p->firstRow,$p->listRows
                                               ");
	   
	 
		}  

        //( id > 1) AND ( ( name like '%thinkphp%') OR ( title like '%thinkphp%') )
        else{
        $count=$showList->where(" ischecked = 1 or ischecked = 2")->count();
	    $p=getpage($count,8);
	    $through=M()->query("select *  from  project
                                                 left JOIN (
                                                            select  p_id.pid,  group_concat(  users.realname separator ';' ) as joinusers
                                                            from  projectbid p_id,  users  where  p_id.uid = users.uid  and p_id.isjoined = 1  GROUP BY  p_id.pid )
                                                 bid on bid.pid = project.id  where ischecked = 1 or ischecked = 2  order by  adddate desc limit $p->firstRow,$p->listRows
                                               ");
        }
      
                                               
		
	    
		$this->assign ( 'through', $through ); // 将查询的值赋值到through
		$this->assign ( 'number', $number ); // 计数器，前台编号累加值
		$this->assign('page',$p->show());
	    $this->display (); // 跳转到showList页面
	}


	// 待审核项目
	public function checkProject() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 3 ); // 验证是否登陆
		
		$number = 1; // 计数器，前台编号累加值
		$checkProject = M ( 'project' ); // 实例化project
		
		if (! empty ( $_POST )) { // 判断表单是否有数据
			$user = M ( 'users' ); // 实例化project
			$id = $_REQUEST ['id'];
			$title = "项目状态通知！";
			$checkinfo = $_POST ['checkinfo'];
			$uid = $checkProject->where ( "id = '$id'" )->getField ( 'uid' );
			$email = $user->where ( "uid = '$uid'" )->getField ( 'email' );
			$content = "项目未通过审核:$checkinfo";
			
			if (SendMail ( $email, $title, $content )) {
				$Hint->mysuccee ( "发送成功!", 1 );
				die ();
			} else {
				$Hint->myerror ( "发送失败,可能是用户未设置正确的邮箱!", 2 );
				die ();
			}
		}
		
		$check = $checkProject->where ( 'ischecked = 0' )->order ( 'adddate desc' )->select (); // 查询未审核项目
		$this->assign ( 'check', $check ); // 将查询的值赋值到check
		$this->assign ( 'number', $number ); // 计数器，前台编号累加值
		
		$this->display (); // 跳转到checkProject页面
	}
	
	// 申报项目
	public function declarePro() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 3 ); // 验证是否登陆
		
		if (! empty ( $_POST )) { // 判断表单是否有数据
			$project = M ( 'project' ); // 实例化project
			$projecttype = M ( 'projecttype' );
			$projectcategory = M ( 'projectcategory' );
			$team = M ( 'team' );
			if (null != $_FILES ['attach'] ['name']) { // 判断文件是否被修改，如果为空则不调用upload方法
				$info = $Hint->upload ( 'attach' ); // 调用upload方法
				$attachname = $info ['attach'] ['savepath'] . $info ['attach'] ['savename']; // 存储附件的子目录和名字
			}
			
			$uid = session ( 'userUid' ); // session取uid
			$isuser = session ( 'userUsername' ); // session取用户名
			$name = $_POST ['name'];
			$leader = $_POST ['leader'];
			$tid = $_POST ['tid'];
			$cid = $_POST ['cid'];
			$teamid = $_POST ['teamid'];
			$intro = $_POST ['intro'];
			$schedule = $_POST ['schedule'];
			$memberinfo = $_POST ['memberinfo'];
			$pstate = $_POST ['pstate'];
			if ($_POST ['isbid'] == 1) { // 判断是否招标
				$date ['biddate'] = $_POST ['biddate'];
				$date ['bidinfo'] = $_POST ['bidinfo'];
				$isbid = 1;
			} else {
				$isbid = 0;
			}
			$ptype = $projecttype->where ( "id=$tid" )->getField ( 'name' );
			$pcategory = $projectcategory->where ( "id=$cid" )->getField ( 'name' );
			$pteam = $team->where ( "id=$teamid" )->getField ( 'name' );
			
			$date ['uid'] = $uid;
			$date ['isuser'] = $isuser;
			$date ['name'] = $name;
			$date ['leader'] = $leader;
			$date ['tid'] = $tid;
			$date ['typename'] = $ptype;
			$date ['cid'] = $cid;
			$date ['categoryname'] = $pcategory;
			$date ['teamid'] = $teamid;
			$date ['teamname'] = $pteam;
			$date ['intro'] = $intro;
			$date ['schedule'] = $schedule;
			$date ['memberinfo'] = $memberinfo;
			$date ['pstate'] = $pstate;
			$date ['isbid'] = $isbid;
			$date ['attach'] = $attachname;
			$date ['adddate'] = date ( "Y-m-d H:i:s" ); // 生成申报日期
			$date ['ischecked'] = 0; // 表示未审核
			
			$result = $project->add ( $date ); // 添加数据
			if ($result) {
				$Hint->mysuccee ( "申报成功，请等待审核！", "2", "Admin/Project/checkProject" );
				die ();
			} else {
				$Hint->myerror ( "申报失败！", "2" );
				die ();
			}
		} else {
			$this->display ();
		}
	}
	
	// 删除项目
	public function projectDel() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 3 ); // 验证是否登陆
		
		$project = M ( 'project' ); // 实例化project
		$projectbid = M ( 'projectbid' ); // 实例化projectbid
		$id = $_REQUEST ['id']; // 取出要删除项目的id
		
		$filename = $project->where ( "id=$id" )->getField ( 'attach' ); // 从数据库中取出附件名
		$file = './Public/UPLOADS/attach/' . "$filename"; // 把目录名存到file中
		unlink ( $file ); // 删除真实数据
		
		$result = $projectbid->where ( "pid=$id" )->delete (); // 根据条件删除招聘人员
		$results = $project->where ( "id=$id" )->delete (); // 根据条件删除项目
		if ($results) {
			echo "0";
			die ();
		} else {
			echo "1";
			die ();
		}
	}
	
	// 导出项目信息页面
	public function exportProject(){
	    $projectcategory = M('projectcategory');
	    $projectcategoryInfo = $projectcategory->select();
	    $this->assign('info',$projectcategoryInfo);
	    $this->display();
	     
	}
	
	// 审核项目异步提交 来自checkProject页面的请求操作
	public function projectExamine() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 3 ); // 验证是否登陆
		$project = M ( 'project' ); // 实例化project
		$id = $_REQUEST ['id'];
		$oid = $_REQUEST ['oid'];
		if ($oid == 1) {
			$return = $project->where ( "id=$id" )->setField ( 'ischecked', 1 );
			if ($return) {
				echo "1";
				die ();
			} else {
				echo "2";
				die ();
			}
		} else {
			$return = $project->where ( "id=$id" )->setField ( 'ischecked', '2' );
			if ($return) {
				echo "1";
				die ();
			} else {
				echo "2";
				die ();
			}
		}
	}
	
	// showList页面 改变“审核未通过”为“审核通过”异步提交方法
	public function showList_project_success() {
		$Hint = new \Operation\Controller\HintController (); // 实例化Hint
		$Hint->checkLogin ( 3 ); // 验证是否登陆
		$project = M ( 'project' ); // 实例化project
		$id = $_REQUEST ['id']; // 获取操作的project id
		$type = $_REQUEST ['type']; // 获得类别，即是审核通过 还是 未通过
		switch ($type) { // 根据类别值做相应的操作 0即为未通过的 1即为通过
			case 0 :
				$data ['ischecked'] = 1;
				if ($project->where ( "id='$id'" )->save ( $data )) { // 修改数据库并判断是否成功并返回值 0即为成功
					echo "0";
				} else {
					echo "1";
				}
				break;
			case 1 :
				$data ['ischecked'] = 2;
				if ($project->where ( "id='$id'" )->save ( $data )) { // 修改数据库并判断是否成功并返回值 1即为失败
					echo "0";
				} else {
					echo "1";
				}
				break;
		}
	}
}